package com.fernfx.xingtan.main.ui;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.BaiduView;
import com.fernfx.xingtan.common.base.BaseFragment;
import com.fernfx.xingtan.contacts.ui.AddFriendsActivity;
import com.fernfx.xingtan.main.contract.ChatContract;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.entity.RobRedPacketEntity;
import com.fernfx.xingtan.main.presenter.ChatPresenter;
import com.fernfx.xingtan.utils.LogUtils;
import com.fernfx.xingtan.utils.NetworkUtil;
import com.fernfx.xingtan.utils.UIUtil;
import com.fernfx.xingtan.view.linearlayout.ShortSlideListener;
import com.fernfx.xingtan.view.linearlayout.SlideBottomLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.fragment.ConversationListFragment;

/**
 * Created by admin on 2018/8/1.
 * 首页
 */
public class ChatFragment extends BaseFragment implements ChatContract.View, View.OnClickListener,View.OnTouchListener{
    @BindView(R.id.chat_rlt)
    RelativeLayout chatClt;
    @BindView(R.id.slideLayout)
    SlideBottomLayout slideVw;
    @BindView(R.id.top_slide_tv)
    TextView topSlideTv;
    @BindView(R.id.baidu_mapVw)
    public TextureMapView baiduMapVw;
    @BindView(R.id.robred_packet_vw)
    TextView robredPacketVw;
    @BindView(R.id.search_edit)
    EditText searchEdit;

    ConversationListFragment conversationListFragment;
    //init presenter
    public ChatContract.Presenter presenter;
    //百度地图操作类
    private BaiduView baiduView;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_chat;
    }

    @Override
    protected void initData() {
        presenter = new ChatPresenter<>();
        presenter.init(this);
        //设置融云重连机制
        presenter.isReconnect(getActivity().getIntent());
        //初始化百度地图
        baiduView = new BaiduView();
        baiduView.initMap(this,baiduMapVw);
    }

    @Override
    protected void initListener() {
        slideVw.setShortSlideListener(new ShortSlideListener() {
            @Override
            public void onShortSlide(float eventY) {
                LogUtils.d("fff");
            }
        });
        searchEdit.setOnTouchListener(this);
    }

    @Override
    protected void initView() {
        conversationListFragment = (ConversationListFragment) getChildFragmentManager().findFragmentById(R.id.conversationlist);
    }

    @Override
    public void onStart() {
        super.onStart();
        baiduView.getBaiduMap().setMyLocationEnabled(true);
        if (!baiduView.getLocationClient().isStarted()) {
            //开启定位
            baiduView.getLocationClient().start();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        baiduMapVw.onResume();

       /* //默认显示
        slideVw.postDelayed(new Runnable() {
            @Override
            public void run() {
                slideVw.show();
            }
        },80);*/
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        // 停止定位功能
        baiduView.getBaiduMap().setMyLocationEnabled(false);
        //停止定位
        baiduView.getLocationClient().stop();

    }

    @Override
    public ConversationListFragment getConversationListFragment() {
        return conversationListFragment;
    }

    @Override
    public ChatFragment getChatFragment() {
        return this;
    }

    @Override
    public void showRobRedPacket(List<RobRedPacketEntity.ObjBean.RecordsBean> records) {
        List<OverlayOptions> options = new ArrayList<OverlayOptions>();
        for (int i = 0;i < records.size();i++) {
            RobRedPacketEntity.ObjBean.RecordsBean recordsBean = records.get(i);
            Bundle mBundle = new Bundle();
            mBundle.putInt("id", recordsBean.getId());
            LatLng point = new LatLng(Double.parseDouble(recordsBean.getLatitude()),Double.parseDouble(recordsBean.getLongitude()));
            //构建Marker图标
            BitmapDescriptor bitmap =  BitmapDescriptorFactory.fromResource(R.mipmap.rob_red_packet_logo);
            // 构建MarkerOption，用于在地图上添加Marker
            OverlayOptions option = new MarkerOptions().position(point).icon(bitmap).zIndex(1) ;
            options.add(option);
            Marker marker = (Marker)(baiduView.getBaiduMap().addOverlay(option));
            marker.setExtraInfo(mBundle);
        }
    }

    @Override
    public void setCircleScope(int scope) {
        OverlayOptions ooCircle = new CircleOptions().fillColor(0x000000FF)
                .center(baiduView.getLoactionLatLng()) .stroke(new Stroke(2, UIUtil.getColor(R.color.gray)))//设置颜色和透明度，均使用16进制显示，0xAARRGGBB，如 0xAA000000 其中AA是透明度，000000为颜色
                .radius(scope);//设置圆的大小
        baiduView.getBaiduMap().addOverlay(ooCircle);
    }

    @Override
    public void requestRobredPacket(BDLocation location) {
        if (NetworkUtil.isConnected(getActivity())) {
            return;
        }
        Map<String, Object> requestArgsMap = new HashMap<>(4);
        requestArgsMap.put("pageNo", 1);
        requestArgsMap.put("size", 10);
//        requestArgsMap.put("longitude", mLongtitude);
//        requestArgsMap.put("latitude", mLantitude);
//                    chatFragment.presenter.requestRobRedPacket(requestArgsMap);
    }


    @OnClick({R.id.robred_packet_vw})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.robred_packet_vw:
                RobredPacketActivity.start(this,baiduView.getCity());
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
            case R.id.search_edit:
                if (AddFriendsActivity.isStartFlag) {
                    AddFriendsActivity.start(getActivity());
                }
                break;
        }
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != baiduMapVw){
            baiduMapVw.onDestroy();
            baiduMapVw = null;
        }
    }
}
